package Doem;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyThreadPoolTest{
    //先有一个阻塞队列
    private BlockingDeque<Runnable> queue=null;
    public MyThreadPoolTest(int n){
        queue = new LinkedBlockingDeque<>(1000);
        for (int i=0;i<n;i++){
            Thread t=new Thread(()->{
                while(true){
                    try {
                        Runnable task=queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }

    }
    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}
public class MyThreadPool {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolTest pool=new MyThreadPoolTest(10);
        for (int i=0;i<100;i++){
            int id=i;
            pool.submit(()->{
                System.out.println(Thread.currentThread().getName()+" id="+id);
            });
        }
    }
}
